package org.PISS.controller;

import org.PISS.controller.model.*;
import org.PISS.services.DAOService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: mafi0813
 * Date: 28.04.14
 * Time: 14:42
 * To change this template use File | Settings | File Templates.
 */
public class ProductController {
    public static List<Product> getProducts(){
        return DAOService.getInstance().getProductDAO().getProducts();
    }

    public static List<Product> getProductsByType(long typeID){
        return DAOService.getInstance().getProductDAO().getProductsByType(typeID);
    }

    public static List<Product> getContainsProducts(long productID){
        return DAOService.getInstance().getProductDAO().getContainsProducts(productID);
    }

    public static List<ProductType> getProductTypes(){
        return DAOService.getInstance().getProductDAO().getProductTypes();
    }

    public static List<Fieldset> getFieldsetsForType(long typeID){
        return DAOService.getInstance().getProductDAO().getFieldsets(typeID);
    }

    public static ProductDetails getProductDetails(long poductID){
        final long product = poductID;
        ProductType productType = DAOService.getInstance().getProductDAO().getProductType(product);

        Map fieldsMap= new HashMap<Long,Field>();
        List<Fieldset> fieldsets = getFieldsetsForType(productType.getTypeID());
        if(fieldsets!=null&&!fieldsets.isEmpty()){
            for(Fieldset fieldset:fieldsets){
                fieldsMap.put(fieldset.getFieldsetId(),DAOService.getInstance().getProductDAO().getField(product,fieldset));
            }
        }

        return new ProductDetails(product,productType,fieldsMap,getContainsProducts(product));
    }
}
